package com.xja.wechat.service.Imp;

import com.xja.wechat.common.Constant;
import com.xja.wechat.common.JsonBean;
import com.xja.wechat.domain.mapper.WX_ShareMapper;
import com.xja.wechat.domain.mapper.WX_UserMapper;
import com.xja.wechat.domain.model.WX_Share;
import com.xja.wechat.domain.model.WX_User;
import com.xja.wechat.service.WechatService;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class WechatServiceImp implements WechatService {
    @Autowired
    private WX_UserMapper wx_userMapper;
    @Autowired
    private WX_ShareMapper wx_shareMapper;

    @Override
    public JsonBean oauth2SnsApi(String code) {
        WX_User wx_user = null;
        JSONObject jsonObject = Constant.scopeOauth2(code);
        if (jsonObject != null) {
            String access_token = (String) jsonObject.get("access_token");
            String scope = (String) jsonObject.get("scope");
            String openid = (String) jsonObject.get("openid");
            System.out.println(openid);
            //根据openid进行查询如果当前用户存在数据库中那么就不需要调用接口获取用户信息反之去调用
            wx_user = wx_userMapper.selectByOpenid(openid);
            if (scope.equals("snsapi_userinfo")) {
                if (wx_user == null) {
                    wx_user = Constant.getUserInfoBySnsapiUserInfo(openid, access_token);
                }
            }
        }
        return new JsonBean(0, "ok", wx_user);
    }

    @Override
    public JsonBean shareData(String targetUrl, WX_User wx_user, String openid) {
        Map<String, Object> map = Constant.jsConfig(targetUrl);
        Integer userId = 0;
        //实现分享链接及后续一些内容
        if (wx_user == null) {
            //wx_user==null---就是说没有从session里取到东西session为空
            WX_User dbUserInfo = wx_userMapper.selectByOpenid(openid);
            userId = dbUserInfo.getId();
        } else {
            userId = wx_user.getId();
        }
        WX_Share wxShare = wx_shareMapper.selectBySenceId(userId);
        Long nowTime = System.currentTimeMillis() / 1000;
        //调用接口之前可以做什么判断
        if (wxShare == null || (nowTime - wxShare.getCreatetime()) > (wxShare.getExpireseconds() - 10)) {
            JSONObject jsonObject = Constant.getQr(userId);
            if (jsonObject.has("ticket")) {
                WX_Share wx_share = new WX_Share();
                String ticket = (String) jsonObject.get("ticket");
                Object expire_seconds = jsonObject.get("expire_seconds");
                String url = (String) jsonObject.get("url");
                String s = expire_seconds.toString();
                wx_share.setCreatetime(System.currentTimeMillis() / 1000);
                wx_share.setExpireseconds(Long.valueOf(s));
                wx_share.setTicket(ticket);
                wx_share.setUrl(url);
                wx_share.setId(userId);
                if (wxShare == null) {
                    wx_shareMapper.insert(wx_share);
                } else {
                    wx_shareMapper.updateByPrimaryKey(wx_share);

                }
                //一旦查出的==null或者过期了   便给赋值
                wxShare = wx_share;
            }
        }
        map.put("ticket", wxShare.getTicket());
        return new JsonBean(0, "ok", map);
    }
}
